package deploy;

import java.util.ArrayList;

import javax.swing.JFrame;

import com.googlecode.grtframework.actuator.RPCServo;
import com.googlecode.grtframework.actuator.IServo;
import com.googlecode.grtframework.actuator.gui.ServoDisplay;
import com.googlecode.grtframework.rpc.RPCConnection;
import com.googlecode.grtframework.sensor.gui.PotentiometerDisplay;
import com.googlecode.grtframework.sensor.gui.SimPotentiometer;
import com.googlecode.grtframework.vis.Displayer;
import com.googlecode.grtframework.vis.MountedPosition;
import com.googlecode.grtframework.vis.RootMount;

import controller.ServoController;

public class Main {

	public static void main(String[] args) {

		RPCConnection connection = null;
		// connection = new InternetRPC("10.1.92.2", 193);
		// try {
		// connection = new USBRPC(0);
		// } catch (NoSuchPortException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (PortInUseException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		Displayer displayer = new Displayer();
		displayer.startPaintLoop();

		JFrame home = new JFrame("grs: robot simulator");
		home.getContentPane().add(displayer);
		home.setVisible(true);
		home.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// servos
		ArrayList<ServoDisplay> servos = new ArrayList<ServoDisplay>();
		MountedPosition servoposition0 = new MountedPosition(RootMount.get(),
				200, 100, 0);
		ServoDisplay servo = new ServoDisplay(displayer, servoposition0);
		servos.add(servo);
		for (int i = 1; i < 10; i++) {
			MountedPosition p = new MountedPosition(servos.get(i - 1), 40, 0, 0);
			servos.add(new ServoDisplay(displayer, p));
		}
		// RPCServo rpcServo = new RPCServo(connection, 12, 2400, +Math.PI / 2,
		// 400, -Math.PI / 2);

		// RPCServo rpcServo = new RPCServo(connection, 12, 682, +Math.PI / 2,
		// 2252, -Math.PI / 2);

		RPCServo rpcServo = new RPCServo(connection, 12, 7, +Math.PI / 2, 2252,
				-Math.PI / 2);

		ArrayList<IServo> servosControlled = new ArrayList<IServo>();
		for (ServoDisplay s : servos) {
			s.startDisplaying();
			servosControlled.add(s);
		}
		servosControlled.add(rpcServo);

		// potentiometer
		MountedPosition potsPosition = new MountedPosition(RootMount.get(),
				300, 400, 0);
		SimPotentiometer pot = new SimPotentiometer(displayer, displayer,
				potsPosition);
		pot.start();
		PotentiometerDisplay potDisplay = new PotentiometerDisplay(displayer,
				pot, potsPosition);
		potDisplay.startDisplaying();
		potDisplay.startListening();

		// controller
		ServoController control = new ServoController(servosControlled, pot);
		control.startListening();
	}
}
